package com.wo.android.pockybill;

import java.util.List;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.wo.android.pockybill.adapter.BillAdapter;
import com.wow.wa.bill.serivce.BillService;
import com.wow.wa.bill.serivce.BillServiceImpl;
import com.wow.wa.bill.serivce.domain.BillDuteDTO;
import com.wow.wa.bill.util.Args;
import com.wow.wa.bill.vo.Bill;
import com.wow.wa.bill.vo.Card;
import com.wow.wa.ui.DialogFactory;

public class BillDuteActivity extends Activity implements OnItemClickListener {

	private static final String TAG = "BillDuteActivity";
	private List<Bill> bills = null;
	private ListView listView = null;

	private BillService service;
	private Button allPayBtn;
	private TextView useMoney;

	private BillDuteDTO dto;

	@Override
	protected void onResume() {

		final NotificationManager notificationManager = (NotificationManager) this
				.getSystemService(Context.NOTIFICATION_SERVICE);
		notificationManager.cancelAll();
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.no_pay_bill);

		this.service = new BillServiceImpl(this);

		this.dto = new BillDuteDTO();
		this.dto.setUseMoney(this.getString(R.string.countNow));

		// ----view--------------------------------------------------
		this.useMoney = (TextView) this.findViewById(R.id.useMoney);
		this.listView = (ListView) this.findViewById(R.id.billListView);

		// --view
		// init-----------------------------------------------------------
		this.useMoney.setText(dto.getUseMoney());

		this.listView.setOnItemClickListener(this);
		// allPayBtn this.service = new BillServiceImpl(this);

		final CollectBill task = new CollectBill();
		task.execute("");

		this.allPayBtn = (Button) this.findViewById(R.id.allPayBtn);
		this.allPayBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				updateAllToPayed();
			}
		});
	}

	public void updateAllToPayed() {

		final DialogInterface.OnClickListener actionListener = new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

				try {

					for (Bill bill : BillDuteActivity.this.bills) {
						bill.setIsPayed(Args.Common.STRING_Y);// 改成以繳交
						BillDuteActivity.this.service.updateBill(bill,
								BillDuteActivity.this);
					}
					final CollectBill task = new CollectBill();
					task.execute("");

				} catch (Exception e) {
					Log.v(TAG, "onItemClick");
				}

			};
		};

		DialogFactory.getAlert(this.getString(R.string.decideAllPayed), this,
				actionListener).show();
		try {

		} catch (Exception e) {
			Log.v(TAG, e.getMessage());
		}
	}

	public class CollectBill extends AsyncTask<String, Void, Integer> {

		@Override
		protected Integer doInBackground(String... params) {
			try {
				bills = BillDuteActivity.this.service
						.getBillWithNoPaid(BillDuteActivity.this);

				final String useMoney = BillDuteActivity.this.service
						.stringByCountTotalMoneyWithNoPay(bills);

				BillDuteActivity.this.dto.setUseMoney(useMoney);

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(Integer result) {

			listView.setAdapter(new BillAdapter(BillDuteActivity.this, bills,
					new Card(), false));

			useMoney.setText(dto.getUseMoney());

			super.onPostExecute(result);
		}

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2,
			long arg3) {

		final DialogInterface.OnClickListener actionListener = new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

				try {

					final Bill bill = bills.get(arg2);
					bill.setIsPayed(Args.Common.STRING_Y);
					service.updateBill(bill, BillDuteActivity.this);
					final CollectBill task = new CollectBill();
					task.execute("");

				} catch (Exception e) {
					Log.v(TAG, "onItemClick");
				}

			};
		};

		DialogFactory.getAlert(this.getString(R.string.decidePayed), this,
				actionListener).show();

		// Toast.makeText(this, "click", Toast.LENGTH_SHORT).show();

	}

}
